import { subscribeWithSelector } from 'zustand/middleware'
import create from 'zustand'
import { fetchCreateMeetingPlanList } from '@/api/create/mix'
import Alert, { Confirm } from '@/components/Alert'
import { t } from 'i18next'
type PlanItemEntity = {
	label: string
	value: string
}
/**转换后的预案数据 */
type TranslatePlanItemEntity = {
	/**预案名称 */
	label: string
	/**对应的预案ID */
	value: string
}
export type DefaultEntity = {
	/**模态框开启弹框*/
	isOpen: boolean
	/**模态框的标题*/
	dialogTitle: string
	/**请求的地址 */
	loadUrl: string
	/**类型*/
	type: 'vmp' | 'mix' | 'tvwall' | 'poll' | 'myTpl' | 'publicTpl'
	/**预案名称 */
	name: string
	/**请求参数*/
	params: {
		order?: number
		count?: number
		start?: number
	}
	/**自定义save方法 */
	self_save: (name: string) => void
	/**自定义over_wite方法 */
	self_over: (name: string, item: PlanItemEntity) => void
}
type StateStore = {
	noPreMsg: {
		vmp: string
		mix: string
		tvwall: string
		poll: string
		myTpl: string
		publicTpl: string
		tpl: string
	}
	/**无预案时候显示文本 */
	emptyMsg: string
	/**预案列表 */
	planOptions: Array<PlanItemEntity>
	/**选中的预案列表*/
	selectPlanItem: PlanItemEntity | null
} & DefaultEntity

type IStore = StateStore & {
	update: (d: Partial<StateStore>) => void
	reset: () => void
	/**初始*/
	init: (_data?: null | Partial<DefaultEntity>) => void
	/**获取没有列表提示语*/
	getNoListMsg: (_d: 'vmp' | 'mix' | 'tvwall' | 'poll' | 'myTpl' | 'publicTpl' | 'tpl') => string
	/**确认方法 */
	handleConfirm: () => void
	/**加载预案列表数据 */
	loadPlanData: () => void
	/**获取无预案时候提示语 */
	getEmptyMsg: () => void
	/**判断是覆盖当前预案还是新增*/
	isOverWrite: () => false | PlanItemEntity
	handlePlanClick: (_d: PlanItemEntity | null) => void
}

const initialState: StateStore = {
	isOpen: false,
	dialogTitle: '',
	noPreMsg: {
		vmp: t('您目前没有画面合成预案'),
		mix: t('您目前没有混音预案'),
		tvwall: t('您目前没有电视墙预案'),
		poll: t('您目前没有轮询预案'),
		myTpl: t('您目前没有会议模板，请先去创建'),
		publicTpl: t('您目前没有会议模板，请先去创建'),
		tpl: t('暂无模板'),
	},
	emptyMsg: '',
	type: 'vmp',
	loadUrl: '',
	selectPlanItem: null,
	planOptions: [{ label: t('不使用预案'), value: '0' }],
	name: '',
	params: {},
	self_save: () => {},
	self_over: () => {},
}
export const useKdPreSettingDialogStore = create<IStore>()(
	subscribeWithSelector((set, get) => ({
		...initialState,
		update: (d) => set({ ...d }),
		reset: () => set(initialState),
		getNoListMsg: (type) => {
			return get().noPreMsg[type]
		},
		/**初始化界面 */
		init: () => {
			const { loadPlanData, getEmptyMsg } = get()
			loadPlanData()
			getEmptyMsg()
		},
		handleConfirm: () => {
			const { name, isOverWrite, self_save, self_over } = get()
			let flag = isOverWrite()
			if (flag) {
				Confirm(t('预案将被覆盖，是否确定要保存？'), function () {
					self_over(name, flag as PlanItemEntity)
					set({ isOpen: false })
				})
			} else {
				self_save(name)
				set({ isOpen: false })
			}
		},
		loadPlanData: async () => {
			const { loadUrl, name, params } = get()
			const res = await fetchCreateMeetingPlanList(loadUrl, params)
			if (res.success && res.data) {
				const planData = res.data
				let arr: Array<TranslatePlanItemEntity> = planData
					? planData.map((item) => ({ label: item.name, value: item.id }))
					: []
				const select = arr.find((item) => {
					return item.label === name
				})
				if (select) {
					set({ planOptions: arr, selectPlanItem: select })
				} else {
					set({ planOptions: arr })
				}
			} else {
				Alert(res.description ?? t('操作失败'))
			}
		},
		getEmptyMsg: () => {
			const { noPreMsg, type } = get()
			set({ emptyMsg: noPreMsg[type] })
		},
		isOverWrite: () => {
			const { name, planOptions } = get()
			const select = planOptions.find((item) => {
				return item.label === name
			})
			if (select) {
				return select
			}
			return false
		},
		handlePlanClick: (item) => {
			set({ selectPlanItem: item })
		},
	}))
)
